/*------------------------------------------------------
| noka flash socket  v1.0 www.97521.com                |
| rebin 2014-11-28                                     |
|-----------------------------------------------------*/
var nksocket = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.id=cfg.id; //id
    	this.rurl=cfg.rurl;
    	this.isdbug=cfg.isbug;
    	this.sendmsg=cfg.sendmsg;//自动发送消息
    	this.interval=cfg.interval;//自动发送间隔
    	this.bodyHtml=$(cfg.id);
    	this.flashID=cfg.id+'_flash';
    	this.openFunction = 'OpenFunction';
    	this.sendFunction = 'sendFunction';
    	this.closeFunction= 'closeFunction';
    	this.setConfig = 'setConfig';
    	this.onDataFunction=cfg.OnDataFunction;//当用数据到达时调用
    	this.onConnection=  cfg.OnConnection;//当连接时调用
    	this.ip = cfg.ip;
    	this.port = cfg.port;
    },
    show : function(){
    	var self = this;
    	self.bodyHtml.innerHTML=self.flashInit();
    	self.eventSocket();
    },
    //----------------初始化flash-----------------------------------------------------------------------------------------------------------
    flashInit : function(){
    	var self = this;
    	var idx = 0;//
    	var html=[];
    	var width='0px';
    	var height='0px';
    	var fun='se='+self.setConfig+'&o='+self.openFunction+'&s='+self.sendFunction+'&c='+self.closeFunction+'&od='+self.onDataFunction+'&oc='+self.onConnection;
    	if(self.isdbug){
    		width='380px';
        	height='380px';
    	}
    	html[idx++]=' <object data="'+self.rurl+'/nokatag/socket/fla/socket.swf?'+fun+'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" name="'+self.flashID+'" id="'+self.flashID+'" align="middle">';
    	html[idx++]=' <param name="allowscriptaccess" value="always" />';
    	html[idx++]=' <param name="allowFullScreen" value="true" />';
    	html[idx++]=' <param name="movie" value="'+self.rurl+'/nokatag/socket/fla/socket.swf?'+fun+'"/>';
    	html[idx++]=' <param name="quality" value="high" />';
    	html[idx++]=' <param neme="wmode" value="opaque"/>';
    	html[idx++]=' <param name="bgcolor" value="#ffffff" />';
    	html[idx++]=' <embed  id="'+self.flashID+'_s" allowscriptaccess="always" allowFullScreen="true" quality="high" wmode="opaque" type="application/x-shockwave-flash"  src="'+self.rurl+'/nokatag/socket/fla/socket.swf?'+fun+'" width="'+width+'" height="'+height+'"   align="middle"/>';
    	html[idx++]=' </object>';
    	return html.join('');
    },
    //---------------事件绑定----------------------------------------------------------------------------------------------------------------
    eventSocket : function(){
    	var self = this;
    	//-----------打开连接-------------------------
    	$(self.id).open = function(ip,port){
    		self.OpenConnection(ip, port);
    	};
    	//-----------发送消息-------------------------
    	$(self.id).send = function(msg){
    		self.sendMsg(msg);
    	};
    	//-----------自动发送------------------------
    	$(self.id).sendTherd = function(msg,th){
    		self.SendTherd(msg, th);
    	};
    	//-----------关闭---------------------------
    	$(self.id).close = function(){
    		self.CloseConnection();
    	};
    	//-----------设置ip和端口---------------------
    	$(self.id).setConfigf = function (ip,port){
    		self.setConfigf(ip,port);
    	};
    	//-----------关闭网页时-----------------------
    	Event.observe(document, 'dom:onunload', function(){
    		self.CloseConnection();//自动关闭连接
    	});
    },
    //---------------------------------------------
    setConfigf : function(ip,port){
    	var self = this;
    	var isClon=false;
    	try{
			$(self.flashID).setConfig(ip,port);
			isClon=true;
		}catch(e){
			try{
				$(self.flashID+'_s').setConfig(ip,port);
				isClon=true;
			}catch(ES){}
		}
		if(!isClon){//连接异常
			setTimeout(function(){
				self.setConfigf(ip, port);
			}, 500);
		}
    },
    //---------------打开连接-------------------------
    OpenConnection : function(ip,port){
    	var isClon=false;
    	var self = this;
		try{
			$(self.flashID).OpenFunction(ip,port);
			isClon=true;
		}catch(e){
			try{
				$(self.flashID+'_s').OpenFunction(ip,port);
				isClon=true;
			}catch(ES){
			}
		}
		if(!isClon){//连接异常
			setTimeout(function(){
				self.OpenConnection(ip, port);
			}, 500);
		}else{//连接成功,启动自动发送消息
			if(undefined!=self.sendmsg && undefined!=self.interval){
				self.SendTherd(self.sendmsg, self.interval);
			}
		}
    },
    //--------------关闭连接-------------------------
    CloseConnection : function(){
    	var self = this;
    	try{
			$(self.flashID).closeFunction();
		}catch(e){
			try{$(self.flashID+'_s').closeFunction();}catch(ES){}
		}
    },
    //-------------发送消息-------------------------
    sendMsg : function(msg){
    	var self = this;
    	try{
			$(self.flashID).sendFunction(msg);
		}catch(e){
			try{$(self.flashID+'_s').sendFunction(msg);}catch(ES){}
		}
    },
    //-------------间隔自动发送-----------------------
    SendTherd : function(msg,th){
    	var self = this;
    	self.sendMsg(msg);
    	setTimeout(function(){
			self.SendTherd(msg,th);
		}, th);
    }
});